package com.prs.crm.dao.cdxh.impl;
import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.cdxh.CdstockDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.cdxh.Cdstock;
import com.prs.crm.domain.cdxh.molding.Molding;
import com.prs.crm.domain.sys.CodeCommon;



public class CdstockDaoImpl extends BaseDaoImpl<Cdstock> implements CdstockDao {

	@Override
	public List<Cdstock> getAllStock() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void saveOrUpdate(Cdstock cdstock) {
			
		this.getHibernateTemplate().saveOrUpdate(cdstock);
	}
	@Override
	public Cdstock getCdstockById(Serializable id) {
		// TODO Auto-generated method stub
		
		//System.out.println(this.getHibernateTemplate().get(Cdstock.class, id)+"aaaaa");
		return this.getHibernateTemplate().get(Cdstock.class, id);
		
		 
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Cdstock getCdstock(final String productCode,final String productName) {
		return (Cdstock)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(Cdstock.class);
				        	criteria.add(Restrictions.eq("productname", productName));
				        	criteria.add(Restrictions.eq("productcode", productCode));				        	
				        	List<Cdstock> result = criteria.list();
				        	if(result.size()==0){
				        		return null;
				        	}else{
				        		return result.get(0);
				        	}
				        }   
				 } );
	}
	@Override
	public void saveOrStock(Cdstock cdStock) {
		this.getHibernateTemplate().saveOrUpdate(cdStock);
		
	}

	
	
}
